/**
 * JAVACC DEMO 1.0
 * @Package com.apache.audit.service.plugins
 */
package com.apache.audit.service.plugins;

import java.util.Map;

import net.sf.json.JSONObject;

import com.apache.api.manager.PluginConnector;
import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.audit.entity.LogModel;
import com.apache.audit.entity.LogModelDetail;
import com.apache.audit.manager.LogModelDetailManager;
import com.apache.audit.manager.LogModelManager;
import com.apache.cache.util.Validator;
import com.apache.tools.ClassToolsUtil;

/**
 * description:  模型属性列表
 * @author dyhou 创建时间：2016-7-11  
 */
public class LogModelExecutePluginImpl implements PluginConnector {

	private LogModelDetailManager logModelDetailManager;
	private LogModelManager logModelManager;

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * 参数:modelId  
	 */
	public Object execute(ParamsVo param) throws Exception {
		ResultEntity entity = new ResultEntity();
		entity.setResult("true");
		String exeType = String.valueOf(param.getParams("exeType"));
		if (Validator.isNull(exeType)) {
			entity.setMessage("缺少方法参数");
		}
		if ("saveModel".equals(exeType)) {
			saveModel(param.getParams(), entity);
		} else if ("delModel".equalsIgnoreCase(exeType)) {
			delModel(param.getParams(), entity);
		} else if ("saveModelDetail".equals(exeType)) {
			saveModelDetail(param.getParams(), entity);
		} else if ("delModelDetail".equalsIgnoreCase(exeType)) {
			delModelDetail(param.getParams(), entity);
		} else if ("modelEntity".equals(exeType)) {
			getModeInfo(param.getParams(), entity);
		} else if ("modelDetailEntity".equals(exeType)) {
			getModeDetailInfo(param.getParams(), entity);
		}

		return entity;
	}

	private void getModeInfo(Map param, ResultEntity entity) {
		String modelId = String.valueOf(param.get("modelId"));
		ParamsVo vo = new ParamsVo();
		vo.setInfoId(modelId);
		LogModel model = (LogModel) logModelManager.getInfoById(vo);
		JSONObject json = JSONObject.fromObject(model);
		entity.setEntity(json);
		entity.setMessage("查询成功");
	}

	private void getModeDetailInfo(Map param, ResultEntity entity) {
		String modelDetailId = String.valueOf(param.get("detailId"));
		ParamsVo vo = new ParamsVo();
		vo.setInfoId(modelDetailId);
		LogModelDetail model = (LogModelDetail) logModelDetailManager.getInfoById(vo);
		JSONObject json = JSONObject.fromObject(model);
		entity.setEntity(json);
		entity.setMessage("查询成功");
	}

	private void saveModel(Map param, ResultEntity entity) {
		LogModel model = new LogModel();
		model = (LogModel) ClassToolsUtil.getInstance().convert2Bean(param, model);
		ParamsVo vo = new ParamsVo();
		vo.setObj(model);
		String modelId = "";
		boolean mark = false;
		if (Validator.isNull(model.getModelId())) {
			modelId = logModelManager.saveInfo(vo);
		} else {
			mark = logModelManager.editInfo(vo);
		}
		if (Validator.isNotNull(modelId) || mark) {
			entity.setEntity(Validator.getDefaultStr(modelId, String.valueOf(mark)));
			entity.setMessage("操作成功");
		} else {
			entity.setMessage("操作失败");
		}
		vo = null;
	}

	private void delModel(Map param, ResultEntity entity) {
		String modelId = String.valueOf(param.get("modelId"));
		ParamsVo vo = new ParamsVo();
		vo.setInfoId(modelId);
		boolean mark = logModelManager.deleteInfo(vo);
		if (mark) {
			entity.setEntity(mark);
			entity.setMessage("操作成功");
		} else {
			entity.setMessage("操作失败");
		}
		vo = null;
	}

	private void saveModelDetail(Map param, ResultEntity entity) {
		LogModelDetail model = new LogModelDetail();
		model = (LogModelDetail) ClassToolsUtil.getInstance().convert2Bean(param, model);
		ParamsVo vo = new ParamsVo();
		vo.setObj(model);
		String modelId = "";
		boolean mark = false;
		if (Validator.isNull(model.getDetailId())) {
			modelId = logModelDetailManager.saveInfo(vo);
		} else {
			mark = logModelDetailManager.editInfo(vo);
		}
		if (Validator.isNotNull(modelId) || mark) {
			entity.setEntity(Validator.getDefaultStr(modelId, String.valueOf(mark)));
			entity.setMessage("操作成功");
		} else {
			entity.setMessage("操作失败");
		}
		vo = null;
	}

	private void delModelDetail(Map param, ResultEntity entity) {
		String detailId = String.valueOf(param.get("detailId"));
		ParamsVo vo = new ParamsVo();
		vo.setInfoId(detailId);
		boolean mark = logModelDetailManager.deleteInfo(vo);
		if (mark) {
			entity.setEntity(mark);
			entity.setMessage("操作成功");
		} else {
			entity.setMessage("操作失败");
		}
		vo = null;
	}

	public void setLogModelDetailManager(LogModelDetailManager logModelDetailManager) {
		this.logModelDetailManager = logModelDetailManager;
	}

	public void setLogModelManager(LogModelManager logModelManager) {
		this.logModelManager = logModelManager;
	}

}
